package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.Entity;
import com.zw.yzk.kabu.data.network.entity.GoodsOrderEntity;
import com.zw.yzk.kabu.data.network.entity.PayEntity;
import com.zw.yzk.kabu.data.network.entity.QueryOrderStateEntity;
import com.zw.yzk.kabu.data.network.task.ConfirmReceiveTask;
import com.zw.yzk.kabu.data.network.task.GoodsOrderTask;
import com.zw.yzk.kabu.data.network.task.PayTask;
import com.zw.yzk.kabu.data.network.task.QueryOrderStateTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.management.ToastManager;
import com.zw.yzk.kabu.mapper.GoodsOrderMapper;
import com.zw.yzk.kabu.utils.WechatPayInfo;
import com.zw.yzk.kabu.view.view.GoodsOrderView;
import com.zw.yzk.kabu.wxapi.WechatManager;

import java.util.ArrayList;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by zhanwei on 2017/3/12.
 */

public class GoodsOrderPresenter implements Presenter {

    private UseCase getGoodsOrder;
    private UseCase confirmReceive;
    private UseCase checkOrderState;
    private UseCase pay;
    private AppCompatActivity activity;
    private GoodsOrderView goodsOrderView;
    private GoodsOrderMapper mapper;
    private String tradeId;

    @Inject
    public GoodsOrderPresenter(@Named("goodsOrderUseCase") UseCase getGoodsOrder,
                               @Named("confirmReceiveUseCase") UseCase confirmReceive,
                               @Named("queryOrderUseCase") UseCase checkOrderState,
                               @Named("payUseCase") UseCase pay,
                               AppCompatActivity activity,
                               GoodsOrderMapper mapper) {
        this.getGoodsOrder = getGoodsOrder;
        this.confirmReceive = confirmReceive;
        this.checkOrderState = checkOrderState;
        this.pay = pay;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(GoodsOrderView view) {
        this.goodsOrderView = view;
    }

    public void getGoodsOrderList(int start, int type) {
        getGoodsOrder.execute(new GoodsOrderSubscriber(activity), new GoodsOrderTask(start, type));
    }

    public void confirmReceive(String orderId) {
        confirmReceive.execute(new ConfirmSubscriber(activity), new ConfirmReceiveTask(orderId));
    }

    public void pay(String orderId) {
        PayTask task = new PayTask();
        task.orderList = new ArrayList<>();
        task.orderList.add(orderId);
        pay.execute(new PaySubscriber(activity), task);
    }

    public void checkOrderState() {
        if (TextUtils.isEmpty(tradeId)) {
            goodsOrderView.confirmReceiveSuccess();
            return;
        }
        checkOrderState.execute(new QueryOrderStateSubscriber(activity), new QueryOrderStateTask(tradeId));
        tradeId = "";
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getGoodsOrder.unSubscribe();
        confirmReceive.unSubscribe();
        pay.unSubscribe();
    }

    private class QueryOrderStateSubscriber extends BaseSubscriber<QueryOrderStateEntity> {

        public QueryOrderStateSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public void onNext(QueryOrderStateEntity entity) {
            super.onNext(entity);
            if (entity.result.result == 1) {
                ToastManager.getInstance().showToast(activity, activity.getResources().getString(R.string.paid_success));
            }
            tradeId = "";
            goodsOrderView.confirmReceiveSuccess();
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            tradeId = "";
        }

        @Override
        public String addErrorMessage() {
            return "";
        }
    }

    private class GoodsOrderSubscriber extends BaseSubscriber<GoodsOrderEntity> {

        public GoodsOrderSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(GoodsOrderEntity entity) {
            super.onNext(entity);
            goodsOrderView.getOrderListSuccessfully(mapper.transfer(entity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            goodsOrderView.getOrderListFailed();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_goods_order_failed);
        }
    }

    private class ConfirmSubscriber extends BaseSubscriber<Entity> {

        public ConfirmSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(Entity entity) {
            super.onNext(entity);
            goodsOrderView.confirmReceiveSuccess();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.confirm_receive_failed);
        }
    }

    private class PaySubscriber extends BaseSubscriber<PayEntity> {

        public PaySubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(PayEntity entity) {
            super.onNext(entity);
            tradeId = entity.result.tradeNo;
            //设置支付信息
            WechatPayInfo.getInstance().setTradeInfo(WechatPayInfo.PAYMENT_TYPE_GOODS, entity.result.tradeNo);
            //调起微信支付
            WechatManager.payByWeChat(activity, entity.result.wechatPay.appId, entity.result.wechatPay.partnerId, entity.result.wechatPay.prepayId,
                    entity.result.wechatPay.packageName, entity.result.wechatPay.nonceStr, entity.result.wechatPay.timeStamp, entity.result.wechatPay.sign);
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.submit_order_failed);
        }
    }
}
